package com.base.vistter.iframe.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.base.vistter.iframe.entity.InverterAlarmDetails;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface InverterAlarmDetailsMapper extends BaseMapper<InverterAlarmDetails> {

    @Select({"<script>",
            "SELECT count(*) FROM inverter_alarm_details iad ",
            "LEFT JOIN inverter_alarm_error iae ON iad.error_code = iae.CODE  AND iae.is_delete = 0 ",
            "LEFT JOIN inverter_devices id ON id.id = iad.inverter_devices_id ",
            "AND id.is_delete = 0 ",
            "LEFT JOIN inverter_station isn ON id.inverter_station_id = isn.id AND isn.is_delete = 0 ",
            "WHERE iad.is_delete = 0 ",
            " <if test='status != null'> AND iad.status = #{status} </if>",
            " <if test='devicesIds != null and !devicesIds.isEmpty()'> AND iad.inverter_devices_id IN ",
            "   <foreach collection='devicesIds' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            "</if>",
            "</script>"})
    Long getCountInverterAlarmDetails(@Param("status") Integer status, @Param("devicesIds") List<Integer> devicesIds);

    @Select({"<script>",
            "SELECT iad.id, iad.error_code, iad.inverter_devices_id, iad.begin_time, iad.end_time, iad.`status`,id.`no`, id.`code`, isn.`name` as station_name, iae.`name`, iae.name_es, iae.solution ",
            "FROM inverter_alarm_details iad ",
            "LEFT JOIN inverter_alarm_error iae ON iad.error_code = iae.CODE  AND iae.is_delete = 0 ",
            "LEFT JOIN inverter_devices id ON id.id = iad.inverter_devices_id ",
            "AND id.is_delete = 0 ",
            "LEFT JOIN inverter_station isn ON id.inverter_station_id = isn.id AND isn.is_delete = 0 ",
            "WHERE iad.is_delete = 0 ",
            " <if test='status != null'> AND iad.status = #{status} </if>",
            " <if test='devicesIds != null and !devicesIds.isEmpty()'> AND iad.inverter_devices_id IN ",
            "   <foreach collection='devicesIds' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            "</if>",
            "ORDER BY iad.`status` ASC, iad.begin_time DESC ",
            "</script>"})
    List<InverterAlarmDetails> getInverterAlarmDetailsList(@Param("status") Integer status, @Param("devicesIds") List<Integer> devicesIds);

    @Select("select iad.id , iad.error_code, iae.`name` , iae.solution from inverter_alarm_details iad left join inverter_alarm_error iae on iad.error_code = iae.`code` and iae.is_delete = 0 where iad.is_delete = 0 and iad.id = #{id}")
    InverterAlarmDetails getInverterAlarmDetailsById(@Param("id") Integer id);

    @Select({"<script>",
            "SELECT iad.*, iae.`name`, iae.name_es, iae.solution FROM ",
            "inverter_alarm_details iad ",
            "LEFT JOIN inverter_alarm_error iae ",
            "on iad.error_code = iae.`code` and iae.is_delete = 0 ",
            "WHERE iad.inverter_devices_id = #{devicesId} and iad.`status` = 1  AND iad.is_delete = 0 ",
            "</script>"
    })
    List<InverterAlarmDetails> getByDevicesId(@Param("devicesId") Integer devicesId);

    @Select({"<script>",
            " select count(*) from inverter_alarm_details iad",
            " left join inverter_devices id",
            " on iad.inverter_devices_id = id.id and id.is_delete = 0",
            " LEFT JOIN inverter_station isn",
            " on id.inverter_station_id = isn.id and isn.is_delete = 0",
            " LEFT JOIN inverter_alarm_error iae",
            " on iad.error_code = iae.`code` and iae.is_delete = 0",
            " where iad.is_delete = 0",
            " <if test='status != null'> AND iad.status = #{status} </if>",
            " <if test='beginTime != null'> AND iad.begin_time &gt;= #{beginTime} </if>",
            " <if test='endTime != null'> AND iad.begin_time &lt;= #{endTime} </if>",
            " <if test='stationIds != null and !stationIds.isEmpty()'> AND isn.id IN ",
            "   <foreach collection='stationIds' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            " </if>",
            "</script>"
    })
    Long pageAlarmDetailsCount(@Param("status") Integer status, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime, @Param("stationIds") List<Integer> stationIds);

    @Select({"<script>",
            " select iad.`status`, iad.grade, isn.name as station_name, id.`no`, id.`code`,iad.error_code,iae.`name`, iae.name_es, iad.begin_time, iad.end_time, iae.solution  from inverter_alarm_details iad",
            " left join inverter_devices id",
            " on iad.inverter_devices_id = id.id and id.is_delete = 0",
            " LEFT JOIN inverter_station isn",
            " on id.inverter_station_id = isn.id and isn.is_delete = 0",
            " LEFT JOIN inverter_alarm_error iae",
            " on iad.error_code = iae.`code` and iae.is_delete = 0",
            " where iad.is_delete = 0",
            " <if test='status != null'> AND iad.status = #{status} </if>",
            " <if test='beginTime != null'> AND iad.begin_time &gt;= #{beginTime} </if>",
            " <if test='endTime != null'> AND iad.begin_time &lt;= #{endTime} </if>",
            " <if test='stationIds != null and !stationIds.isEmpty()'> AND isn.id IN ",
            "   <foreach collection='stationIds' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            " </if>",
            " order by iad.begin_time desc",
            " limit #{start},#{size}",
            "</script>"
    })
    List<InverterAlarmDetails> pageAlarmDetails(@Param("status") Integer status, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime, @Param("stationIds") List<Integer> stationIds, @Param("start") Long start ,@Param("size") Long size);

    @Select({"<script>",
        "select iad.id, iad.error_code, iad.grade, iad.begin_time, iad.end_time ,  iae.`name` , iae.solution  from inverter_alarm_details iad ",
                "left join inverter_alarm_error iae on iad.error_code = iae.`code` and iae.is_delete = 0 ",
                "where iad.is_delete = 0 ",
                " <if test='inverterAlarmId != null'> AND iad.inverter_alarm_id = #{inverterAlarmId} </if>",
        "</script>"
    })
    List<InverterAlarmDetails> getListByAlarmId(@Param("inverterAlarmId") Integer inverterAlarmId);

}
